package com.example.wendao.view.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.ShareCompat;
import androidx.core.widget.NestedScrollView;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;

import com.example.wendao.R;
import com.example.wendao.databinding.FragmentViewPagerDetailTestBinding;

public class ViewPagerDetailTestFragment extends Fragment {

    private FragmentViewPagerDetailTestBinding binding;

    boolean isToolbarShown = false;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_view_pager_detail_test, container, false);

        Bundle bundle = getArguments();

        binding.plantDetailScrollview.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                boolean shouldShowToolbar = scrollY > binding.toolbar.getHeight();
                if (ViewPagerDetailTestFragment.this.isToolbarShown != shouldShowToolbar) {
                    ViewPagerDetailTestFragment.this.isToolbarShown = shouldShowToolbar;
                    binding.appbar.setActivated(shouldShowToolbar);
                    binding.toolbarLayout.setTitleEnabled(shouldShowToolbar);
                }
            }
        });

        binding.toolbar.setNavigationOnClickListener(v -> {
            Navigation.findNavController(v).navigateUp();
        });

        binding.toolbar.setOnMenuItemClickListener(item -> {
            if (item.getItemId() == R.id.action_share) {
                createShareIntent();
                return true;
            }
            return false;
        });

        return binding.getRoot();
    }

    private void createShareIntent(){
        String shareText = "分享文本";
        Intent shareIntent = ShareCompat.IntentBuilder.from(getActivity())
                .setText(shareText)
                .setType("text/plain")
                .createChooserIntent()
                .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
        startActivity(shareIntent);
    }




}

